package vn.orderonline.core.redis;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import vn.orderonline.core.redis.model.RedisBasketDrink;
import vn.orderonline.core.redis.model.RedisBasketFood;
import vn.orderonline.core.redis.model.RedisServiceHelper;

@Service
public class RedisOrderService extends RedisService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisOrderService.class);
	
	public List<RedisBasketFood> getAllBasketFood(Set<Integer> foodIds) {
		LOGGER.info("*** getBasketFood() ***");
		if (foodIds.isEmpty()) {
			return null;
		}
		Set<String> foodKeys = new HashSet<>();
		String foodKey = null;
		for (Integer foodId : foodIds) {
			foodKey = RedisServiceHelper.formatKey(RedisServiceHelper.FOODPREFIX, foodId.toString());
			foodKeys.add(foodKey);
		}
		List<RedisBasketFood> list = mget(foodKeys, RedisBasketFood.class);
		LOGGER.info("*** getBasketFood() ** --> END");
		return list;
	}
	
	public RedisBasketFood getRedisBasketFoodByFoodId(Integer foodId) {
		LOGGER.info("*** getBasketFood() *** ");
		if (foodId == null) {
			return null;
		}
		String foodKey = RedisServiceHelper.formatKey(RedisServiceHelper.FOODPREFIX, foodId.toString());
		return get(foodKey, RedisBasketFood.class);
	}

	public void saveBasketFood(RedisBasketFood basketFood) {
		LOGGER.info("*** saveBasketFood() ***");
		if (basketFood.getFoodId() == null) {
			return;
		}
		String foodKey = RedisServiceHelper.formatKey(RedisServiceHelper.FOODPREFIX, basketFood.getFoodId().toString());
		set(foodKey, basketFood);
		LOGGER.info("*** saveBasketFood() *** --> END"); 
	}
	
	public void removeBasketFood(Integer foodId) {
		LOGGER.info("*** removeBasketFood(%d) **", foodId);
		if (foodId == null) {
			return;
		}
		String foodKey = RedisServiceHelper.formatKey(RedisServiceHelper.FOODPREFIX, foodId.toString());
		delete(foodKey);
		LOGGER.info("*** removeBaksetFood(%d) *** --> END", foodId);
	}
	
	public Boolean existFood(Integer foodId) {
		LOGGER.info("*** existFood(%d) ***", foodId);
		if (foodId == null) {
			return false;
		}
		String foodKey = RedisServiceHelper.formatKey(RedisServiceHelper.FOODPREFIX, String.valueOf(foodId));
		Boolean isContain = contains(foodKey);
		LOGGER.info("*** existFood(%d) *** --> END", foodId);
		return isContain;
	}
	
	// area drink
	public void saveBasketDrink(RedisBasketDrink basketDrink) {
		LOGGER.info("*** saveBasketDrink() ***");
		if (basketDrink.getDrinkId() == null) {
			return;
		}
		String drinkKey = RedisServiceHelper.formatKey(RedisServiceHelper.DRINKPREFIX, String.valueOf(basketDrink.getDrinkId()));
		set(drinkKey, basketDrink);
		LOGGER.info("*** saveBasketDrink() *** -> END");
	}

}
